<template>
  <view class="container">
    <view class="list-container">
      <view v-if="recordList.length === 0 && status !== 'loading'" class="empty-state">{{$t('acupoint.empty')}}</view>

      <view v-else class="record-card" v-for="item in recordList" :key="item.id" @click="goToDetail(item.appoint_no)">
        <view class="card-left">
          <view class="result"><img :src="item.pic_url" class="result-img" /></view>
          <text class="part-name-text">{{ item.part_name }}</text>
        </view>
        <view class="card-right">
          <text class="date-text">{{ item.update_datetime }}</text>
        </view>
      </view>

      <!-- 加载更多区域 -->
      <view class="custom-load-more">
        <view v-if="status === 'loading'">
          <text class="load-more-text">{{$t('acupoint.loading')}}</text>
        </view>
        <view v-if="status === 'noMore'">
          <text class="load-more-text">—— {{$t('acupoint.no_more')}} ——</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { acupointList } from '@/api/acupoint.js'

export default {
  data() {
    return {
      recordList: [],
      page: 1,
      pageSize: 10,
      status: 'more' // 'more', 'loading', 'noMore'
    }
  },
  onLoad() {
    this.getList()
    this.setTitle()
  },
  onShow() {
    this.setTitle()
  },
  // 上拉触底事件
  onReachBottom() {
    // console.log('onReachBottom triggered, status:', this.status)
    if (this.status === 'more') {
      this.getList()
    }
  },
  // 下拉刷新事件
  onPullDownRefresh() {
    // console.log('onPullDownRefresh triggered')
    this.resetAndReload()
  },
  methods: {
    setTitle() {
      // 国际化页面标题
      if (this.$t) {
        uni.setNavigationBarTitle({
          title: this.$t('acupoint.title')
        })
      }
    },
    resetAndReload() {
      // console.log('resetAndReload called')
      this.page = 1
      this.recordList = []
      this.status = 'more'
      this.getList()
    },
    async getList() {
      // console.log('getList called, page:', this.page, 'status:', this.status)
      
      if (this.status !== 'more') {
        // console.log('getList early return, status is not more')
        return
      }
      
      this.status = 'loading'

      try {
        const params = {
          page: this.page,
          pageSize: this.pageSize
        }

        // console.log('API request params:', params)
        const res = await acupointList(params)
        // console.log('API response:', res)

        if (res.code === 200 && res.success) {
          const newList = res.result.items || []
          // console.log('New list length:', newList.length)

          // 追加新数据到现有列表
          this.recordList = [...this.recordList, ...newList]
          // console.log('Total recordList length:', this.recordList.length)

          if (newList.length < this.pageSize) {
            this.status = 'noMore'
            // console.log('No more data, status set to noMore')
          } else {
            this.status = 'more'
            this.page++
            // console.log('More data available, page incremented to:', this.page)
          }
        } else {
          uni.showToast({ 
            title: res.message || this.$t('common.data_load_failed'), 
            icon: 'none' 
          })
          this.status = 'more'
        }
      } catch (error) {
        // console.error('API Error:', error)
        uni.showToast({ 
          title: this.$t('common.502BadRequest'), 
          icon: 'none' 
        })
        this.status = 'more'
      } finally {
        uni.stopPullDownRefresh()
      }
    },
    goToDetail(appointNo) {
      uni.navigateTo({
        url: `/pages/record/detail/index?id=${appointNo}&source=acupoint`
      })
    }
  }
}
</script>

<style lang="scss">
page {
  background-color: #f5f5f5;
}

.container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* 列表样式 */
.list-container {
  padding: 24rpx;
  flex: 1;
}

.record-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #ffffff;
  border-radius: 16rpx;
  padding: 32rpx;
  margin-bottom: 24rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.04);
}

.card-left {
  flex: 1;
  display: flex;
  align-items: center;
  margin-right: 30rpx;
  
  .result {
    margin-right: 60rpx;
  }
  
  .result-img {
    height: 100rpx;
    width: auto;
  }

  .part-name-text {
    font-size: 24rpx;
    color: #333333;
  }
}

.card-right {
  .date-text {
    font-size: 24rpx;
    color: #999999;
  }
}

// 空状态
.empty-state {
  text-align: center;
  color: #999;
  padding-top: 200rpx;
  font-size: 28rpx;
}

.custom-load-more {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20rpx 0;
  min-height: 80rpx;

  .load-more-text {
    font-size: 28rpx;
    color: #999999;
  }
}
</style>
